package com.igoal.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.igoal.dto.UserDTO;
import com.igoal.dto.UserGoalEntryDTO;
import com.igoal.dto.UserGoalEntryDoneDTO;
import com.igoal.service.CommonService;
import com.igoal.service.GoalEntryManager;
import com.igoal.service.GoalManager;
import com.igoal.service.MessageManager;
import com.igoal.service.UserManagerExt;
import com.igoal.web.model.BaseModel;
import com.igoal.web.model.EntryDetailModel;

@Controller
@RequestMapping("/viewEntry.html")
public class EntryDetailController extends BaseController {

	public String getViewPrefix() {
		return BaseControllerDelegate.DEFAULT_VIEW_PREFIX;
	}
	
	@Autowired
	public void setGoalEntryManager(GoalEntryManager goalEntryManager) {
		this.goalEntryManager = goalEntryManager;
	}

	@Autowired
	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}

	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}

	@Autowired
	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}
	
	@Autowired
	public void setGoalManager(GoalManager goalManager) {
		this.goalManager = goalManager;
	}

	@RequestMapping(method = RequestMethod.GET)
	public String showEntryDetail(ModelMap modelMap, HttpServletRequest request,
			@RequestParam("id") Long entryId, @RequestParam("entryType") Integer type) {
		UserDTO user = null;
		UserDTO entryOwner = null;

		if (isLoginUser()) {
			user = getUserSession();
		}

		EntryDetailModel model = new EntryDetailModel();

		if (type == 1) {
			UserGoalEntryDTO entry = goalEntryManager.getEntry(entryId, user);
			entryOwner = userManagerExt.get(entry.getOwnerId());
			model.setEntry(entry);
		} else {
			UserGoalEntryDoneDTO entryDone = goalEntryManager.getEntryDone(entryId, user);
			entryOwner = userManagerExt.get(entryDone.getOwnerId());
			model.setEntryDone(entryDone);
		}

		if (isLoginUser()) {
			simpleModelInitialize(user, model);
		} else {
			initUserAvatar(entryOwner, model);
			initUserStatus(entryOwner, model);
			initRightZone(entryOwner, model);
			model.setUsername(entryOwner.getUsername());
		}

		model.setFamousQuote(commonService.getRandomFamousQuote());

		initPeopleYouMayKnow(user, model);
		applyModel(modelMap, model);
		return getViewPrefix() + "entryDetail";
	}

	public void initTopPage(BaseModel model, HttpServletRequest request) {
		// TODO Auto-generated method stub
		
	}

	public void initRightPage(BaseModel model, HttpServletRequest request) {
		// TODO Auto-generated method stub
		
	}

	public void initLeftPage(BaseModel model, HttpServletRequest request) {
		// TODO Auto-generated method stub
		
	}

}
